package com.oauth.global.client.ui;

import java.util.List;

import com.google.gwt.core.client.GWT;
import com.google.gwt.event.dom.client.ClickEvent;
import com.google.gwt.uibinder.client.UiBinder;
import com.google.gwt.uibinder.client.UiField;
import com.google.gwt.uibinder.client.UiHandler;
import com.google.gwt.user.client.Window;
import com.google.gwt.user.client.ui.Button;
import com.google.gwt.user.client.ui.Composite;
import com.google.gwt.user.client.ui.FlowPanel;
import com.google.gwt.user.client.ui.Image;
import com.google.gwt.user.client.ui.TextArea;
import com.google.gwt.user.client.ui.VerticalPanel;
import com.google.gwt.user.client.ui.Widget;
import com.oauth.components.client.rpc.OAuthAsnyCallBack;
import com.oauth.global.client.beans.ApplicationImageUrl;
import com.oauth.global.client.rpc.GlobalRemoteServiceAsync;

public class StatusUpdate extends Composite {

	private static StatusUpdateUiBinder uiBinder = GWT.create(StatusUpdateUiBinder.class);

	interface StatusUpdateUiBinder extends UiBinder<Widget, StatusUpdate> {
	}

	@UiField
	Button updateButton;

	@UiField
	VerticalPanel statusUpdatePanel;

	@UiField
	FlowPanel imagePanel;

	@UiField
	TextArea textArea;

	private GlobalRemoteServiceAsync mGlobalRemoteServiceAsync;

	public StatusUpdate() {
		initWidget(uiBinder.createAndBindUi(this));
	}

	public void setService(GlobalRemoteServiceAsync pGlobalRemoteServiceAsync) {
		mGlobalRemoteServiceAsync = pGlobalRemoteServiceAsync;
	}

	public void initImages() {
		mGlobalRemoteServiceAsync
				.getApplicationImagesForStatus(new OAuthAsnyCallBack<List<ApplicationImageUrl>>() {

					@Override
					public void onSuccess(List<ApplicationImageUrl> pImageList) {
						if (pImageList != null) {
							if (pImageList.isEmpty()) {
								statusUpdatePanel.setVisible(false);
							} else {
								for (ApplicationImageUrl url : pImageList) {
									Image img = new Image(url.getImageUrl());
									img.setTitle(url.getName());
									img.setSize("20px", "20px");
									imagePanel.add(img);
								}
							}
						}
					}
				});
	}

	@UiHandler("updateButton")
	void onClick(ClickEvent e) {
		updateButton.setEnabled(false);
		mGlobalRemoteServiceAsync.updateStatus(textArea.getText(),
				new OAuthAsnyCallBack<Boolean>() {

					@Override
					public void onSuccess(Boolean pArg0) {
						Window.alert("Success.............");
						updateButton.setEnabled(true);
					}
				});
	}

}
